package com.stlm2.controller;

import com.stlm2.dems.service.KeFuService;
import com.stlm2.dems.service.SeckillGoodsService;
import com.stlm2.util.codecontent.CodeContent;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by apple on 2017.12.22.
 */
@Controller
@RequestMapping("/seckillGoodsC")
public class SeckillGoodsController {


    @Resource
    private SeckillGoodsService seckillGoodsService;


    @RequestMapping(value="/listView",produces="application/json;charset=UTF-8")
    public ModelAndView listView(ModelAndView modelAndView){
        modelAndView.setViewName("/seckillGoods/list");
        return modelAndView;
    }


    @RequestMapping(value="/addView",produces="application/json;charset=UTF-8")
    public ModelAndView addView(ModelAndView modelAndView){
        modelAndView.setViewName("/seckillGoods/add");
        return modelAndView;
    }


    @RequestMapping(value="/detailsView",produces="application/json;charset=UTF-8")
    public ModelAndView detailsView(ModelAndView modelAndView,String id){
        modelAndView.setViewName("/seckillGoods/details");
        modelAndView.addObject("id",id);
        return modelAndView;
    }



    /**
     * 添加秒杀商品
     * ffl
     * 2017-12-22 10:43:42
     * @param token
     * @param goodsId
     * @param goodsSpecificationId
     * @param seckillPrice
     * @param seckillDate
     * @param inventory
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/add.json",produces="application/json;charset=UTF-8")
    public String addSeckillGoods(String token,String goodsId,String goodsSpecificationId,String seckillPrice,String seckillDate,String inventory){
        String data=null;
        try {
            data=seckillGoodsService.addSeckillGoods(token, goodsId, goodsSpecificationId, seckillPrice, seckillDate, inventory);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"添加失败",data);
        }
        return  data;
    }

    /**
     * 编辑 秒杀商品 价格和库存
     * ffl
     * 2017-12-22 13:59:21
     * @param token
     * @param id
     * @param seckillPrice
     * @param inventory
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/edit.json",produces="application/json;charset=UTF-8")
    public String editSeckillGoods(String token ,String id ,String seckillPrice,String inventory){
        String data=null;
        try {
            data=seckillGoodsService.editSeckillGoods(token, id, seckillPrice, inventory);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"编辑失败",data);
        }
        return  data;
    }


    /**
     * 查询list
     * ffl
     * 2017-12-22 11:55:29
     * @param token
     * @param goodsId
     * @param goodsSpecificationId
     * @param seckillDate
     * @param state
     * @param pageNu
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/list.json",produces="application/json;charset=UTF-8")
    public String search(String token, String goodsId,String goodsSpecificationId,String seckillDate,String goodsName,String abbreviation,String state,String pageNu,String pageSize){

        String data = null;
        try {
            data = seckillGoodsService.search(token, goodsId, goodsSpecificationId, seckillDate, goodsName,abbreviation,state, pageNu,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return CodeContent.getModelByCode(0, "查询失败", data);
        }
        return data;
    }


    /**
     * 获取秒杀商品详情
     * ffl
     * 2017-12-22 11:59:32
     * @param token
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getCrmDetails.json",produces="application/json;charset=UTF-8")
    public String  getDetails(String token,String id ){
        String data = null;
        try {
            data = seckillGoodsService.getDetails(token, id);
        } catch (Exception e) {
            e.printStackTrace();
            return CodeContent.getModelByCode(0, "查询失败", data);
        }
        return data;
    }

    /**
     * 上下架秒杀商品
     *ffl
     * 2017-12-22 14:08:24
     * @param token
     * @param state
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/updateState.json",produces="application/json;charset=UTF-8")
    public  String editState(String token ,Boolean state,String id){
        String data = null;
        try {
            data = seckillGoodsService.editState(token, state, id);
        } catch (Exception e) {
            e.printStackTrace();
            return CodeContent.getModelByCode(0, "操作失败", data);
        }
        return data;
    }

}
